/*
 * list.c
 *
 *  Created on: 2021年3月14日
 *      Author: pc
 */

#include <stdio.h>
#include <stdlib.h>

typedef struct {
	char stuName[10];
	int Age;
} stuInfo, ElemType;

typedef struct node {
	ElemType data;
	struct node *next;
} ListNode, *ListPtr;

void inputNode(ListNode*q)
{
	printf("Enter the name of student:");
	scanf("%s",q->data.stuName);
	printf("Enter the age of student:");
	scanf("%d", &q->data.Age);
}

ListNode *CreateList()
{
	ListNode *p =(ListNode *)malloc(sizeof(ListNode));
	p->data.Age=0;
	p->next=NULL;
	printf("A new empty list was created successfully\n");
	return p;
}

void PrintList(ListNode *p)
{
	int c = 0;
    while(p->next)
    {
    	c++;
    	printf("%d %s %d\n",c, p->next->data.stuName, p->next->data.Age);
    	p=p->next;
    }
    if(c==0)
    	printf("This is an empty list!\n");
}

void InsertList(ListNode *p)
{
	ListNode *q =(ListNode *)malloc(sizeof(ListNode));
	inputNode(q);
	if(p->next)
	{
		q->next=p->next;
        p->next=q;
	}
	else
		p->next=q;
	printf("student named %s inserted successfully!\n", q->data.stuName);
}

//释放内存
void ClearList(ListNode*p)
{
	while(p!=NULL){
		ListNode*t=p;
		p=p->next;
		free(t);
	}
}

int
main(int argc, char *argv[])
{
	ListNode *ListHead = NULL;
	while(1)
	{
		printf("1 Create List\n");
		printf("2 Printf List\n");
		printf("3 Insert List\n");
		printf("4 Quit\n");
		int command;
		scanf("%d", &command);
		switch(command)
		{
		case 1:
            ListHead = CreateList();
		    break;
		case 2:
		    if(/* ListHead->data.Age==0 */ListHead!=NULL)
		        PrintList(ListHead);
		    else
		    	printf("Please create a list first!\n");
		    break;
		case 3:
		    if(/* ListHead->data.Age==0 */ListHead!=NULL)
		    	InsertList(ListHead);
		    else
		        printf("Please create a list first!\n");
		    break;
		case 4:
			ClearList(ListHead);
		    return 0;
		}
	}
	return 0;
}
